package com.fc.test.service;

import java.util.List;
import java.util.stream.Collectors;

import com.fc.test.mapper.custom.TableConfigDao;
import com.fc.test.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.fc.test.common.base.BaseService;
import com.fc.test.common.support.Convert;
import com.fc.test.mapper.auto.TableConfigMapper;
import com.fc.test.model.auto.TableConfig;
import com.fc.test.model.auto.TableConfigExample;
import com.fc.test.model.custom.Tablepar;
import com.fc.test.util.SnowflakeIdWorker;

/**
 * Service
* @Title: TableConfigService.java 
* @Package com.fc.test.service 
* @author fuce
* @email 87766867@qq.com
* @date 2019-07-29 13:47:58  
 */
@Service
public class TableConfigService implements BaseService<TableConfig, TableConfigExample>{
	@Autowired
	private TableConfigMapper tableConfigMapper;

	@Autowired
	private TableConfigDao tableConfigDao;

    /**
     * 分页查询
     * @param tablepar
     * @param name
     * @return
     */
	 public PageInfo<TableConfig> list(Tablepar tablepar,String name){
	        TableConfigExample testExample=new TableConfigExample();
	        String sortName = StringUtils.isEmpty(tablepar.getSortName()) ? "id":tablepar.getSortName();
	        testExample.setOrderByClause(sortName + " ASC");
	        if(name!=null&&!"".equals(name)){
	            String likeName = "%" + name + "%" ;
	        	testExample.or(testExample.createCriteria().andTaskNameLike(likeName).andSourceTableLike(likeName)
                .andTargetTableLike(likeName));
	        }

	        PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
	        List<TableConfig> list= tableConfigMapper.selectByExample(testExample);
	        PageInfo<TableConfig> pageInfo = new PageInfo<TableConfig>(list);
	        return  pageInfo;
	 }

	@Override
	public int deleteByPrimaryKey(String ids) {
		List<String> lista=Convert.toListStrArray(ids);
		List<Integer> listb = lista.stream().map(Integer::parseInt).collect(Collectors.toList());
		TableConfigExample example=new TableConfigExample();
		example.createCriteria().andIdIn(listb);
		return tableConfigMapper.deleteByExample(example);
	}
	
	
	@Override
	public TableConfig selectByPrimaryKey(String id) {
		
		return tableConfigMapper.selectByPrimaryKey(Integer.parseInt(id));
	}

	
	@Override
	public int updateByPrimaryKeySelective(TableConfig record) {
		return tableConfigMapper.updateByPrimaryKeySelective(record);
	}
	
	/**
	 * 添加
	 */
	@Override
	public int insertSelective(TableConfig record) {
		//添加雪花主键id
		return tableConfigMapper.insertSelective(record);
	}
	
	@Override
	public int updateByExampleSelective(TableConfig record, TableConfigExample example) {
		
		return tableConfigMapper.updateByExampleSelective(record, example);
	}

	
	@Override
	public int updateByExample(TableConfig record, TableConfigExample example) {
		
		return tableConfigMapper.updateByExample(record, example);
	}

	@Override
	public List<TableConfig> selectByExample(TableConfigExample example) {
		
		return tableConfigMapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(TableConfigExample example) {
		
		return tableConfigMapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(TableConfigExample example) {
		
		return tableConfigMapper.deleteByExample(example);
	}
	
	/**
	 * 检查name
	 * @param tableConfig
	 * @return
	 */
	public int checkNameUnique(TableConfig tableConfig){
		TableConfigExample example=new TableConfigExample();
		example.createCriteria().andTaskNameEqualTo(tableConfig.getTaskName());
		List<TableConfig> list=tableConfigMapper.selectByExample(example);
		return list.size();
	}

    public List<TableConfig> selectAll() {
        return tableConfigDao.selectAll();
    }
}
